<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
<#assign classNameAllLower = className?lower_case>
<#assign hasParentSearchField = 'false'>
<#if adminSearchField?? && adminSearchField != "">
    <#list adminSearchField?split(",") as field>
        <#if field?contains("_") && field?contains(".")>
            <#assign hasParentSearchField = 'true'>
            <#break>
        </#if>
    </#list>
</#if>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${basePackage}.${moduleName}.dao.${className}Dao">
<#if hasParentSearchField = 'true'>

    <select id="list" resultType="com.wing.${moduleName}.model.entity.${className}">
        select
        <#list table.columns as column>
            <#if column.sqlName == 'version' || column.sqlName == 'update_time' || column.sqlName == 'is_deleted' || column.sqlName == 'remark'>
            <#elseif column.sqlTypeName == "LONGTEXT" || column.sqlTypeName == "TEXT" || column.sqlTypeName == "MEDIUMTEXT">
            <#else>
            <#if column_index != 0>,</#if>t.${column.sqlName} as ${column.columnNameLower}
            </#if>
        </#list>
        <#if ancestorTableList?? && (ancestorTableList?size>0)>
            <#list ancestorTableList as ancestorTable>
                <#if adminSearchField?? && adminSearchField != "">
                    <#list adminSearchField?split(",") as field>
                        <#if field?contains(ancestorTable.sqlName)>
                            <#list ancestorTable.columns as ancestorColumn>
                                <#if ancestorColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
            ,${ancestorTable.sqlName?substring(ancestorTable.sqlName?length-3, ancestorTable.sqlName?length)}.${ancestorColumn.sqlName} as ${ancestorTable.className?uncap_first}${ancestorColumn.columnName}
                                    <#break>
                                </#if>
                            </#list>
                        </#if>
                    </#list>
                </#if>
            </#list>
        </#if>
        <#if parentTableList?? && (parentTableList?size>0)>
            <#list parentTableList as parentTable>
                <#if adminSearchField?? && adminSearchField != "">
                    <#list adminSearchField?split(",") as field>
                        <#if field?contains(parentTable.sqlName)>
                            <#list parentTable.columns as parentColumn>
                                <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
            ,${parentTable.sqlName?substring(parentTable.sqlName?length-3, parentTable.sqlName?length)}.${parentColumn.sqlName} as ${parentTable.className?uncap_first}${parentColumn.columnName}
                                    <#break>
                                </#if>
                            </#list>
                        </#if>
                    </#list>
                </#if>
            </#list>
        </#if>
        from
        ${table.sqlName} t
            <#if parentTableList?? && (parentTableList?size>0)>
                <#list parentTableList as parentTable>
                    <#assign length = parentTable.sqlName?length>
            left join ${parentTable.sqlName} ${parentTable.sqlName?substring(length-3, length)}
                    on ${parentTable.sqlName?substring(length-3, length)}
                    .id=t.${parentTable.sqlName?substring(parentTable.sqlName?index_of("_")+1, length)}_id
                </#list>
            </#if>
            <#if ancestorTableList?? && (ancestorTableList?size>0)>
                <#list ancestorTableList as ancestorTable>
                    <#assign length = ancestorTable.sqlName?length>
                    <#assign prefix = "t">
                    <#if ancestorTable_has_next>
                        <#assign nextAncestorTable = ancestorTableList[ancestorTable_index+1]>
                        <#assign prefix = nextAncestorTable.sqlName?substring(nextAncestorTable.sqlName?length-3, nextAncestorTable.sqlName?length)>
                    </#if>
            left join ${ancestorTable.sqlName} ${ancestorTable.sqlName?substring(length-3, length)}
                    on ${ancestorTable.sqlName?substring(length-3, length)}.id= ${prefix}
                    .${ancestorTable.sqlName?substring(ancestorTable.sqlName?index_of("_")+1, length)}_id
                </#list>
            </#if>
        where 1=1
    <#list table.columns as column>
        <#if column.pk>
        <#elseif column.sqlName == 'version' || column.sqlName == 'update_time' || column.sqlName == 'is_deleted' || column.sqlName == 'remark'>
        <#elseif column.isDateTimeColumn>
        <if test="params != null and params.start${column.columnName} != null">and t.create_time <#noparse>>=
            #{params.start</#noparse>${column.columnName}<#noparse>}</#noparse></if>
        <if test="params != null and params.end${column.columnName} != null">and t.create_time
                <#noparse><![CDATA[ <= ]]> #{params.end</#noparse>${column.columnName}<#noparse>}</#noparse></if>
        <#elseif column.columnNameLower?contains('type') || column.columnNameLower?contains('Type') || column.columnNameLower?contains('status') || column.columnNameLower?contains('Status') || column.columnNameLower?contains('Default') || column.columnNameLower?contains('Delete') || column.columnNameLower?contains('plusMinus') || column.columnNameLower?contains('Id') || column.sqlName == 'sort'>
            <#if column.sqlTypeName != "INT">
        <if test="params != null and params.${column.columnNameLower} != null">and t.${column.sqlName}
            = ${r'#{params.'}${column.columnNameLower}}
        </if>
            <#else>
        <if test="params != null and params.${column.columnNameLower} != null and params.${column.columnNameLower} != ''">
            and t.${column.sqlName} = ${r'#{params.'}${column.columnNameLower}}
        </if>
            </#if>
        <#elseif column.sqlTypeName != "LONGTEXT" && column.sqlTypeName != "TEXT" && column.sqlTypeName != "MEDIUMTEXT">
        <if test="params != null and params.${column.columnNameLower} != null and params.${column.columnNameLower} != ''">
            and t.${column.sqlName} like CONCAT('%','${r'${params.'}${column.columnNameLower}}','%')
        </if>
        </#if>
    </#list>
    <#if parentTableList?? && (parentTableList?size>0)>
        <#list parentTableList as parentTable>
            <#if adminSearchField?? && adminSearchField != "">
                <#list adminSearchField?split(",") as field>
                    <#if field?contains(parentTable.sqlName)>
                        <#list parentTable.columns as parentColumn>
                            <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                <#if parentColumn.isEnum>
        <if test="params != null and params.${parentTable.className?uncap_first}${parentColumn.columnName} != null and params.${parentTable.className?uncap_first}${parentColumn.columnName} != ''">
            and ${parentTable.sqlName?substring(parentTable.sqlName?length-3, parentTable.sqlName?length)}
            .${parentColumn.sqlName} = ${r'#{params.'}${parentTable.className?uncap_first}${parentColumn.columnName}}
        </if>
                                <#elseif parentColumn.sqlTypeName != "LONGTEXT" && parentColumn.sqlTypeName != "TEXT" && parentColumn.sqlTypeName != "MEDIUMTEXT">
        <if test="params != null and params.${parentTable.className?uncap_first}${parentColumn.columnName} != null and params.${parentTable.className?uncap_first}${parentColumn.columnName} != ''">
            and ${parentTable.sqlName?substring(parentTable.sqlName?length-3, parentTable.sqlName?length)}
            .${parentColumn.sqlName} like
            CONCAT('%','${r'${params.'}${parentTable.className?uncap_first}${parentColumn.columnName}}','%')
        </if>
                                </#if>
                                <#break>
                            </#if>
                        </#list>
                    </#if>
                </#list>
            </#if>
        </#list>
    </#if>
        <choose>
            <when test="params != null and params.sort != null and params.sort.trim() != ''">
                order by t.${r'${params.sort}'} ${r'${params.order}'}
            </when>
            <otherwise>
                order by t.id desc
            </otherwise>
        </choose>
    </select>
</#if>

    <select id="list4app" resultType="com.wing.${moduleName}.model.response.${className}VO">
        select
        <#list table.columns as column>
            <#if column.sqlName == 'version' || column.sqlName == 'update_time' || column.sqlName == 'is_deleted' || column.sqlName == 'remark'>
            <#elseif column.sqlTypeName == "LONGTEXT" || column.sqlTypeName == "TEXT" || column.sqlTypeName == "MEDIUMTEXT">
            <#else>
            <#if column_index != 0>,</#if>t.${column.sqlName} as ${column.columnNameLower}
            </#if>
        </#list>
        <#if parentTableList?? && (parentTableList?size>0)>
            <#list parentTableList as parentTable>
                <#if adminSearchField?? && adminSearchField != "">
                    <#list adminSearchField?split(",") as field>
                        <#if field?contains(parentTable.sqlName)>
                            <#list parentTable.columns as parentColumn>
                                <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
            ,${parentTable.sqlName?substring(parentTable.sqlName?length-3, parentTable.sqlName?length)}.${parentColumn.sqlName} as ${parentTable.className?uncap_first}${parentColumn.columnName}
                                    <#break>
                                </#if>
                            </#list>
                        </#if>
                    </#list>
                </#if>
            </#list>
        </#if>
        from
        ${table.sqlName} t
            <#if parentTableList?? && (parentTableList?size>0)>
                <#list parentTableList as parentTable>
                    <#assign length = parentTable.sqlName?length>
            left join ${parentTable.sqlName} ${parentTable.sqlName?substring(length-3, length)} on ${parentTable.sqlName?substring(length-3, length)}.id = t.${parentTable.sqlName?substring(parentTable.sqlName?index_of("_")+1, length)}_id
                </#list>
            </#if>
        where 1=1
<#list table.columns as column>
    <#if column.pk>
    <#elseif column.sqlName == 'version' || column.sqlName == 'update_time' || column.sqlName == 'is_deleted' || column.sqlName == 'remark'>
    <#elseif column.isDateTimeColumn>
        <if test="params != null and params.start${column.columnName} != null">and t.create_time <#noparse>>=#{params.start</#noparse>${column.columnName}<#noparse>}</#noparse></if>
        <if test="params != null and params.end${column.columnName} != null">and t.create_time<#noparse><![CDATA[ <= ]]> #{params.end</#noparse>${column.columnName}<#noparse>}</#noparse></if>
    <#elseif column.columnNameLower?contains('type') || column.columnNameLower?contains('Type') || column.columnNameLower?contains('status') || column.columnNameLower?contains('Status') || column.columnNameLower?contains('Default') || column.columnNameLower?contains('Delete') || column.columnNameLower?contains('plusMinus') || column.columnNameLower?contains('Id') || column.sqlName == 'sort'>
        <#if column.sqlTypeName != "INT">
        <if test="params != null and params.${column.columnNameLower} != null">and t.${column.sqlName}= ${r'#{params.'}${column.columnNameLower}}</if>
        <#else>
        <if test="params != null and params.${column.columnNameLower} != null and params.${column.columnNameLower} != ''">and t.${column.sqlName} = ${r'#{params.'}${column.columnNameLower}}</if>
        </#if>
    <#elseif column.sqlTypeName != "LONGTEXT" && column.sqlTypeName != "TEXT" && column.sqlTypeName != "MEDIUMTEXT">
        <if test="params != null and params.${column.columnNameLower} != null and params.${column.columnNameLower} != ''">and t.${column.sqlName} like CONCAT('%','${r'${params.'}${column.columnNameLower}}','%')</if>
    </#if>
</#list>
        <choose>
            <when test="params != null and params.sort != null and params.sort.trim() != ''">
                order by t.${r'${params.sort}'} ${r'${params.order}'}
            </when>
            <otherwise>
                order by t.id desc
            </otherwise>
        </choose>
    </select>

    <select id="get4appById" resultType="com.wing.${moduleName}.model.response.${className}VO">
        select
    <#list table.columns as column>
        <#if column.sqlName == 'version' || column.sqlName == 'update_time' || column.sqlName == 'is_deleted' || column.sqlName == 'remark'>
        <#else>
            <#if column_index != 0>,</#if>t.${column.sqlName} as ${column.columnNameLower}
        </#if>
    </#list>
        from
            ${table.sqlName} t
        where t.id= <#noparse>#{value}</#noparse>
    </select>

</mapper>
